package com.kangaroo.myhome.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.kangaroo.myhome.mapper.TSysMenuMapper;
import com.kangaroo.myhome.model.TSysOptionExample;
import com.kangaroo.myhome.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kangaroo.myhome.mapper.TSysMenuMapper;
import com.kangaroo.myhome.model.CommonSearch;
import com.kangaroo.myhome.model.TSysMenu;
import com.kangaroo.myhome.model.TSysMenuExample;
import com.kangaroo.myhome.vo.ResponseModel;

import tk.mybatis.mapper.util.StringUtil;

@Service
public class MenuService {

    @Autowired
    private TSysMenuMapper tSysMenuMapper;

    /**
     * 
     * @param name
     * @param page
     * @param rows
     * @return
     */
    public ResponseModel findAll(String name, int page, int rows) {
        ResponseModel model = new ResponseModel();

        CommonSearch user = new CommonSearch();
        user.setString1(name);
        user.setPagination(page, rows);
        List<TSysMenu> users = tSysMenuMapper.selectByPagination(user);
        long count = tSysMenuMapper.selectByPaginationCount(user);
        model.setCode(200);
        model.setMessage("数据获取成功");
        model.setDataDataGrid(users, count);
        return model;
    }

    /**
     * @param tSysMenu
     * @return
     */
    public ResponseModel edit(TSysMenu tSysMenu) {

        ResponseModel model = new ResponseModel();

        if (tSysMenu == null) {
            model.setCode(201);
            model.setMessage("参数异常");
            return model;
        }

        int result = 0;
        String id = tSysMenu.getId();
        if (StringUtil.isEmpty(id)) {
            TSysMenu subWayExist = view(tSysMenu);
            if (subWayExist != null) {
                model.setCode(201);
                model.setMessage("菜单：【" + tSysMenu.getName() + "】已经存在！");
                return model;
            } else {
                tSysMenu.setCreateTime(new Date());
                tSysMenu.setId(UUID.randomUUID().toString());
                tSysMenu.setCreator(SecurityUtils.getUsername());
                result = tSysMenuMapper.insertSelective(tSysMenu);
            }

        } else {
            result = tSysMenuMapper.updateByPrimaryKeySelective(tSysMenu);
        }

        model.setCode(200);
        model.setMessage("操作成功");
        return model;
    }

    /**
     * @param tSysMenu
     * @return
     */
    public TSysMenu view(TSysMenu tSysMenu) {
        ResponseModel model = new ResponseModel();

        TSysMenuExample example = new TSysMenuExample();
        String id = tSysMenu.getId();
        String name = tSysMenu.getName();
        TSysMenu resultSubWay = null;
        if (StringUtil.isNotEmpty(id)) {
            resultSubWay = tSysMenuMapper.selectByPrimaryKey(id);
            return resultSubWay;
        }
        if (StringUtil.isNotEmpty(name)) {
            example.createCriteria().andNameEqualTo(name);
            List<TSysMenu> users = tSysMenuMapper.selectByExample(example);
            if (users != null && users.size() > 0) {
                resultSubWay = users.get(0);
            }
        }

        return resultSubWay;
    }

    public void delete(TSysMenu tSysMenu) {
        tSysMenuMapper.deleteByPrimaryKey(tSysMenu.getId());

        TSysMenuExample option = new TSysMenuExample();
        option.createCriteria().andPIdEqualTo(tSysMenu.getId());
        tSysMenuMapper.deleteByExample(option);
    }

}
